package com.wisdytech.linkdcs.linkdcsservicepad.pad.action;


import com.alibaba.fastjson.JSON;
import com.wisdytech.app.quality.vo.EquipmentVO;
import com.wisdytech.app.quality.vo.ManufactureVO;
import com.wisdytech.app.quality.vo.OperationVO;
import com.wisdytech.app.quality.vo.SysUserVO;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionRecord;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.linkdcsservicepad.common.BaseUtils;
import com.wisdytech.linkdcs.linkdcsservicepad.common.DateUtil;
import com.wisdytech.linkdcs.linkdcsservicepad.pad.service.IssueService;
import com.wisdytech.linkdcs.pad.model.Issue;
import com.wisdytech.linkdcs.pad.model.IssueAttachment;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  品质异常前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-08-15
 */
@Controller
@RequestMapping("/app/issue")
public class IssueAction {

    @Value("${img.localtion}")
    private String location;

    @Autowired
    private IssueService issueService;



    /**
     * <p>Description: 查询品质异常登记</p>
     * @throws Exception 异常
     */
    @RequestMapping(value = "/getIssueRecord.mvc")
    @ResponseBody
    public StandardResult getIssueRecord(Filter filter) {
        List<Issue> list = this.issueService.getIssueRecord(filter);
        return StandardResult.getSuccessResult(list);
    }

    /**
     * <p>Description: 查询品质异常待处理记录</p>
     * @throws Exception 异常
     */
    @RequestMapping(value = "/getIssueSolveRecord.mvc")
    @ResponseBody
    public StandardResult getIssueSolveRecord(String issueStatus,Filter filter) {
        List<Issue> list = this.issueService.getIssueRecord(issueStatus,filter);
        return StandardResult.getSuccessResult(list);
    }

    /**
     * 保存异常登记对象
     * @param issueData
     * @param username
     * @param file
     * @return
     */
    @RequestMapping("/saveIssueRecord.mvc")
    @ResponseBody
    public StandardResult saveIssueRecord(@RequestParam("issue")String  issueData,
                                          @RequestParam("username")String username,@RequestParam(value = "img",required = false) MultipartFile file) {
        //保存文件
        String contentType = file.getContentType();   //图片文件类型
        String fileName = file.getOriginalFilename();  //图片名字
        String truename= BaseUtils.getUUID()+"."+fileName;
        String relativePath = DateUtil.getCurDateDefaultStr();
        try {
            boolean imgResult=BaseUtils.uploadFile(file.getBytes(), location+"/"+relativePath, truename);
            if(!imgResult) {
                return StandardResult.getFailResult("图片保存失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return StandardResult.getFailResult("图片保存失败");
        }

        Issue issue = JSON.parseObject(issueData, Issue.class);
        IssueAttachment issueAttachment = new IssueAttachment();
        issueAttachment.setAttachmentName(fileName);
        issueAttachment.setAttachmentPath(relativePath+"/"+truename);
        this.issueService.saveIssueRecord(issue,issueAttachment,username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 分析异常
     * @param issueData
     * @param username
     */
    @ResponseBody
    @RequestMapping(value = "/analyseIssue.mvc")
    public StandardResult analyseIssue(String issueData,String username){
        Issue issue = JSON.parseObject(issueData, Issue.class);
        this.issueService.analyseIssue(issue, username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 评审异常
     * @param issueData
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/reviewIssue.mvc")
    public StandardResult reviewIssue(String issueData,String username){
        Issue issue = JSON.parseObject(issueData, Issue.class);
        this.issueService.reviewIssue(issue, username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 决定异常
     * @param issueData
     * @param username
     */
    @ResponseBody
    @RequestMapping(value = "/decideIssue.mvc")
    public StandardResult decideIssue(String issueData,String username){
        Issue issue = JSON.parseObject(issueData, Issue.class);
        this.issueService.decideIssue(issue, username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 解决异常
     * @param issueData
     * @param username
     */
    @ResponseBody
    @RequestMapping(value = "/solveIssue.mvc")
    public StandardResult solveIssue(String issueData,String username){
        Issue issue = JSON.parseObject(issueData, Issue.class);
        this.issueService.solveIssue(issue,username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 确认异常
     * @param issueData
     * @param username
     */
    @ResponseBody
    @RequestMapping(value = "/confirmIssue.mvc")
    public StandardResult confirmIssue(String issueData,String username){
        Issue issue = JSON.parseObject(issueData, Issue.class);
        this.issueService.confirmIssue(issue,username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 关闭异常
     * @param issueId
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/closeIssue.mvc")
    public StandardResult closeIssue(String issueId,String username){
        this.issueService.closeIssue(issueId, username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 通过产线获取工序
     * @param lineId
     * @return
     */
    @RequestMapping(value = "/getOperationBylineId.mvc")
    @ResponseBody
    public StandardResult getOperationBylineId(String lineId) {
        List<OperationVO> list = this.issueService.getOperationBylineId(lineId);
        return StandardResult.getSuccessResult(list);
    }

    /**
     * 通过产线id及工序id获取工序
     * @param lineId
     * @param opId
     * @return
     */
    @RequestMapping(value = "/getOperationByLineCOde.mvc")
    @ResponseBody
    public StandardResult getEquipByLineIdAndOpId(String lineId,String opId) {
        List<EquipmentVO> list = this.issueService.getEquipByLineIdAndOpId(lineId,opId);
        return StandardResult.getSuccessResult(list);
    }

    /**
     * 通过生产订单获取产品编号及产线
     * @param manufactureCode
     * @return
     */
    @RequestMapping(value = "/getManufactureByCode.mvc")
    @ResponseBody
    public StandardResult getManufactureByCode(String manufactureCode) {
        ManufactureVO vo = this.issueService.getManufactureByCode(manufactureCode);
        return StandardResult.getSuccessResult(vo);
    }

    /**
     * <p>Description: 获取责任人</p>
     */
    @RequestMapping(value = "/getUser.mvc")
    @ResponseBody
    public StandardResult getUser() {
        List<SysUserVO> listVO = this.issueService.getUser();
        return StandardResult.getSuccessResult(listVO);
    }


}

